/***
 * 
 */
import java.awt.FlowLayout;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;

import javax.media.CannotRealizeException;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.sound.sampled.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.media.format.UnsupportedFormatException;
import javax.media.protocol.DataSource;
import javax.media.rtp.InvalidSessionAddressException;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JProgressBar;
import javax.media.GainControl;


public class New_Cam extends JFrame{
	private JPanel panelik = null;// new JPanel();
	private Player player = null;
	 private ActionListener closeListener;
	String sz_imie="Nieznany";
	static Properties wlasciwosci; 
	Closer cl = new Closer();
	
	
	
	
	 public void   szukajIP(String ip){   
		 int n =  Komunikator.lista.getSize();    
		 String [] TablicaIP = new String[n];   
		 for (int i = 0; i < n; i++) {
         String word = (String)Komunikator.lista.getElementAt(i);
           String sz_ip=word.substring(word.indexOf("*")+1,word.indexOf("/"));           
           TablicaIP[i]=sz_ip;;              
           if (ip.equals(TablicaIP[i])) {                          
        	   sz_imie=word.substring(0,word.indexOf("*")); }if 
        	   (!ip.equals(TablicaIP[i])){};                                 }                        }	 
	 
public class repley extends Thread{
	repley(){
		start();
	}
public void	start(){

	
		String tab[] = new String [1];
	tab[0]="REPLEY*"+Komunikator.repley_IP+"/4444/4448";		
		
		try {
	
			Komunikator.newTalk(tab,true);
		} catch (InvalidSessionAddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Komunikator.tr_counter++;
}


}
	 
	 
public New_Cam(DataSource ds,String transmision_typ,final JButton closeButton,String tty) throws CannotRealizeException{
	
	try {
 		Socket s = new Socket(Komunikator.repley_IP, 8177);
 		 PrintWriter out = new PrintWriter(s.getOutputStream());
 	
 		out.println("??&&*"+Komunikator.sam_adres);
 		 
 		out.flush();
 		}
 		catch(IOException e)
 		{}	 

	try {
		player = javax.media.Manager.createRealizedPlayer(ds);
	} catch (NoPlayerException e) {
	System.out.println("Wyjatek");
		e.printStackTrace();
	} catch (IOException e) {
		System.out.println("Wyjatek");
		e.printStackTrace();
	}
	

	 this.panelik = new JPanel();
	 panelik.setLayout(new BorderLayout());
	 
	 if (transmision_typ=="video"){
	 setSize(330,305);
	 setLocation(400,200);
	 setResizable(true);
	 addWindowListener(cl);
	 //dla transmisji audio zakomentowane 
	 panelik.add(this.player.getVisualComponent(), BorderLayout.CENTER);
	 panelik.add(this.player.getControlPanelComponent(), BorderLayout.SOUTH);
	 
	 wlasciwosci=PropertyLoader.load("conf/System.properties");
	 
	 if (Komunikator.act_o_connection.contains(Komunikator.repley_IP)==false){
	
		if (wlasciwosci.getProperty("AUTO.REPLY").contains("TAK")){
			repley r1 = new repley();
		}

	 }
	}
	 else {
		 setSize(330,50);
		 setLocation(400,505);
		 setResizable(false);
		 panelik.add(this.player.getControlPanelComponent());
		 addWindowListener(cl);		 
	 }
	 this.player.start();
         Container contener = getContentPane();

         contener.add(panelik);

	
	szukajIP(Komunikator.repley_IP);
        setTitle("Rozmowa: "+ sz_imie);

       closeListener = new ActionListener() {
             public void actionPerformed(ActionEvent event)
             {
            	 
                closeButton.removeActionListener(closeListener);
                
  
                dispose();
             }
          };
       closeButton.addActionListener(closeListener);


}
	
class Closer extends WindowAdapter {

	   public void windowClosing(WindowEvent e) {
		   dispose();
	   
	   }

}

}



/**
 * @author celmar
 *
 */


